﻿'*******************************************************************************************
'* Copyright Brügmann Software GmbH, Papenburg
'* Author: Björn Kremer
'* Contact: codeplex<at>patorg.de
'* 
'* VB.CMIS is a VB.NET implementation of the Content Management Interoperability Services (CMIS) standard
'*
'* This file is part of VB.CMIS.
'* 
'* VB.CMIS is free software: you can redistribute it and/or modify
'* it under the terms of the GNU Lesser General Public License as published by
'* the Free Software Foundation, either version 3 of the License, or
'* (at your option) any later version.
'* 
'* VB.CMIS is distributed in the hope that it will be useful,
'* but WITHOUT ANY WARRANTY; without even the implied warranty of
'* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'* GNU Lesser General Public License for more details.
'* 
'* You should have received a copy of the GNU Lesser General Public License
'* along with VB.CMIS. If not, see <http://www.gnu.org/licenses/>.
'*******************************************************************************************
Imports System.Xml.Serialization
Imports System.Runtime.Serialization
Imports CMISObjectModel.BaseTypes

Namespace TypeDefinitions

   <XmlRoot("type", Namespace:=CmisNamespaceConstants.Cmisra)>
   Public MustInherit Class TypeDefinition
      Inherits BaseDefinition
      Implements IBaseDefinition
      Implements IXmlSerializable




      Public Property PropertyDefinitions() As List(Of PropertyDefinitions.IPropertyDefinition)

      Public MustOverride ReadOnly Property BaseId() As BaseIdEnum

      Public Property ParentId() As String

      Public Property Creatable() As Boolean

      Public Property Fileable() As Boolean

      Public Property FulltextIndexed() As Boolean

      Public Property IncludedInSupertypeQuery() As Boolean

      Public Property ControllablePolicy() As Boolean

      Public Property ControllableAcl() As Boolean

      Public MustOverride ReadOnly Property CMISTypeName As String

      Public Sub New(id As String, localName As String, displayName As String, queryName As String, PropertyDefinitions As List(Of PropertyDefinitions.IPropertyDefinition), parentId As String)
         MyBase.New(id, localName, displayName, queryName)
         Me.PropertyDefinitions = PropertyDefinitions
         Me.ParentId = parentId
      End Sub

      Public Sub New()

      End Sub


      Public Function GetAtomLinks(baseUri As Uri, repositoryId As String) As List(Of CMISLink)

         Dim cmisTypeId As String = Me.Id
         Dim retVal As New List(Of CMISLink)

         retVal.Add(New CMISLink(New Uri(baseUri, ServiceURIs.GetTypeDefinition.Replace("{repositoryId}", repositoryId).Replace("{id}", cmisTypeId).TrimStart("/"c)), CmisAtomLinkConstants.Self, CmisContentTypeConstants.Entry))
         retVal.Add(New CMISLink(New Uri(baseUri, ServiceURIs.GetServiceDocumentById.Replace("{repositoryId}", repositoryId).TrimStart("/"c)), CmisAtomLinkConstants.Service, CmisContentTypeConstants.Service))
         retVal.Add(New CMISLink(New Uri(baseUri, ServiceURIs.GetTypeDefinition.Replace("{repositoryId}", repositoryId).Replace("{id}", Common.GetDataMemberValueFromEnum(BaseId, GetType(BaseIdEnum))).TrimStart("/"c)), CmisAtomLinkConstants.DescribedBy, CmisContentTypeConstants.Entry))
         retVal.Add(New CMISLink(New Uri(baseUri, ServiceURIs.GetTypeChildrenBase1.Replace("{repositoryId}", repositoryId).Replace("{id}", cmisTypeId).TrimStart("/"c)), CmisAtomLinkConstants.Down, CmisContentTypeConstants.Feed))
         retVal.Add(New CMISLink(New Uri(baseUri, ServiceURIs.GetTypeDescendantsBase1.Replace("{repositoryId}", repositoryId).Replace("{id}", cmisTypeId).TrimStart("/"c)), CmisAtomLinkConstants.Down, CmisContentTypeConstants.Tree))
         If Not String.IsNullOrEmpty(ParentId) Then
            retVal.Add(New CMISLink(New Uri(baseUri, ServiceURIs.GetTypeDefinition.Replace("{repositoryId}", repositoryId).Replace("{id}", ParentId).TrimStart("/"c)), CmisAtomLinkConstants.Up, CmisContentTypeConstants.Entry))
         End If
         Return retVal
      End Function

  
      Public Function GetSchema() As System.Xml.Schema.XmlSchema Implements System.Xml.Serialization.IXmlSerializable.GetSchema
         Return Nothing
      End Function

      Public Sub ReadXml(reader As System.Xml.XmlReader) Implements System.Xml.Serialization.IXmlSerializable.ReadXml

      End Sub

      Public Overridable Sub WriteXml(writer As System.Xml.XmlWriter) Implements System.Xml.Serialization.IXmlSerializable.WriteXml
         Dim ns As String = CmisNamespaceConstants.Cmis
         Dim nsra As String = CmisNamespaceConstants.Cmisra
         Dim nsw3 As String = CmisNamespaceConstants.W3instance
         writer.WriteAttributeString("type", CmisNamespaceConstants.W3instance, Me.CMISTypeName)


         'Element order seems to be relevant!!!
         writer.WriteElementString("id", ns, Me.Id)
         writer.WriteElementString("localName", ns, Me.LocalName)
         writer.WriteElementString("localNamespace", ns, Me.LocalNamespace)
         writer.WriteElementString("displayName", ns, Me.DisplayName)
         writer.WriteElementString("queryName", ns, Me.QueryName)
         writer.WriteElementString("description", ns, Me.Description)
         writer.WriteElementString("baseId", ns, Common.GetDataMemberValueFromEnum(Me.BaseId, GetType(BaseIdEnum)))
         If Not String.IsNullOrEmpty(Me.ParentId) Then
            writer.WriteElementString("parentId", ns, Me.ParentId)
         End If
         writer.WriteElementString("creatable", ns, CStr(Me.Creatable).ToLowerInvariant)
         writer.WriteElementString("fileable", ns, CStr(Me.Fileable).ToLowerInvariant)
         writer.WriteElementString("queryable", ns, Me.Queryable.ToString.ToLowerInvariant)
         writer.WriteElementString("fulltextIndexed", ns, CStr(Me.FulltextIndexed).ToLowerInvariant)
         writer.WriteElementString("includedInSupertypeQuery", ns, CStr(Me.IncludedInSupertypeQuery).ToLowerInvariant)
         writer.WriteElementString("controllablePolicy", ns, CStr(Me.ControllablePolicy).ToLowerInvariant)
         writer.WriteElementString("controllableACL", ns, CStr(Me.ControllableAcl).ToLowerInvariant)

         For Each prop As IXmlSerializable In Me.PropertyDefinitions
            Dim serializer As New XmlSerializer(prop.GetType)
            serializer.Serialize(writer, prop)
         Next
      End Sub


      Public Enum BaseIdEnum

         <EnumMember(Value:="cmis:document")> _
         CmisDocument

         <EnumMember(Value:="cmis:folder")> _
         CmisFolder

         <EnumMember(Value:="cmis:relationship")> _
         CmisRelationship

         <EnumMember(Value:="cmis:policy")> _
         CmisPolicy
      End Enum

      
   End Class
End Namespace